# -*-coding:Utf-8 -* 
""" Animation module
Part of common module
"""

from time import time
from .events import *
		
class AnimateValue(BasicEvent):
	
	layerDefault = 0
	
	def __init__(self, value, duration = 0):
		BasicEvent.__init__(self)
		self._time = self._default_time = duration
		self._value = value
		self._start = 0
		self._end = 0
		self.layer = AnimateValue.layerDefault
		self.update = AnimateValue._update
	"""	
	def __get__(self, instance, owner):
		return self._value
		
	def __set__(self, instance, value):
		if type(value) == tuple:
			self.anime(value[0],value[1])
		else:
			self.anime(value,self._default_time)
		"""
	def __EV_update__(self, *param):
		actualT = time()
		progress = (actualT-self._EV_TIME_time_)/self._time
		if progress >= 1.0:
			self.stop()
			return
		self.update(self,progress)
		self._EV_TIME_lastupdate_ = time()
	
	def _update(self,progress):
		self.stop()
		
	def linearUpdate(self,progress):
		self._value = self._start+((self._end-self._start)*progress)
		
	@property
	def value(self):
		return self._value
		
	@value.setter
	def value(self, value):
		if type(value) == tuple:
			self.anime(value[0],value[1])
		else:
			self.anime(value,self._default_time)
			
	def anime(self, value, duration, curve=linearUpdate):
		if self.active:
			self._EV_PreviousEvent._EV_NextEvent = self._EV_NextEvent
			self._EV_NextEvent._EV_PreviousEvent = self._EV_PreviousEvent
		self._start = self._value
		self._end = value
		self._time = duration
		if callable(curve):
			self.update = curve
		mEvt = MASTER_EVENT(self.layer)
		self._EV_TIME_time_ = time()
		self._EV_PreviousEvent = mEvt._EV_PreviousEvent
		self._EV_PreviousEvent._EV_NextEvent = self
		mEvt._EV_PreviousEvent = self
		self._EV_NextEvent = mEvt
		self.active = True
		return self
	
	def activate(self, kamikaze = None):
		return
		
	def stop(self):
		self.update = AnimateValue._update
		self._EV_PreviousEvent._EV_NextEvent = self._EV_NextEvent
		self._EV_NextEvent._EV_PreviousEvent = self._EV_PreviousEvent
		self._value = self._end
		self._start = self._end = 0
		self.active = False
		return self
		
class Animate(BasicEvent):
	
	layerDefault = 0
	
	def linearUpdate(self,progress):
		self.dico[self.key] = self._start+((self._end-self._start)*progress)
	
	def __init__(self,dico,key,start,end,duration,curve=linearUpdate):
		self._time = duration
		self.dico = dico
		self.key = key
		self._start = start
		self._end = end
		self.update = curve
		mEvt = MASTER_EVENT(Animate.layerDefault)
		self._EV_TIME_time_ = time()
		self._EV_PreviousEvent = mEvt._EV_PreviousEvent
		self._EV_PreviousEvent._EV_NextEvent = self
		mEvt._EV_PreviousEvent = self
		self._EV_NextEvent = mEvt
		
	def __bool__(self):
		return True
		
	def __EV_update__(self, *param):
		actualT = time()
		progress = (actualT-self._EV_TIME_time_)/self._time
		if progress >= 1.0:
			self.dico[self.key] = self._end
			self.stop()
			return
		self.update(self,progress)
		self._EV_TIME_lastupdate_ = time()
	
	def _update(*args):
		return
	
	def stop(self):
		self.update = self._update
		self._EV_PreviousEvent._EV_NextEvent = self._EV_NextEvent
		self._EV_NextEvent._EV_PreviousEvent = self._EV_PreviousEvent
		del self
	
	
